<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\facade\Session;
use think\Db;
use think\facade\Config;

/*新闻事件*/
class Events extends Base
{  
    /*列表*/
	  public function index(Request $request){
		   $this->check_session();
        /*左侧导航*/
        $data_get=$request->get()['nav'];

        $data=$this->navleft($data_get);

        $this->assign($data);

        return $this->fetch();
	  }

    public function get_data(Request $request){
        $this->check_session();
        $get=$request->get();
        
        $model=new \app\admin\model\Events();/*调用模型查询*/
        
        $model_data=$model->get_data($get['page'],$get['limit']);
        
      
        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
       
    }

      /*新增 或者 修改 页面*/
    public function change(Request $request){
        $this->check_session();
        /*左侧导航*/
        $data_get=$request->get();
        $data=$this->navleft($data_get['nav']);
        if(isset($data_get['id'])){
          $model=new \app\admin\model\Events();/*调用模型查询*/
          $model_data=$model->get_edit($data_get['id']);
          $data['data_list']=$model_data;
        }
        $events=Db::connect('publicdata')->table('lily_eventsclass')->order('class_id','desc')->select();
        $author=Db::connect('publicdata')->table('lily_admin')->where(['is_editor'=>1])->order('id','desc')->select();
        $data['author']=$author;
        $data['events']=$events;
        $this->assign($data);
        return $this->fetch();
    }

	    /*执行 新增 或者 修改任务*/
	  public function do_change(Request $request){
		    $this->check_session();
        $data=$request->post();
        if(empty($data['title'])){
          $result=['status'=>'0','msg'=>'请填写标题'];
          return json($result);
        }
          if(empty($data['author'])){
              $result=['status'=>'0','msg'=>'请选择作者'];
              return json($result);
          }
        if(empty($data['class_zhi'])){
          $result=['status'=>'0','msg'=>'请选择分类'];
          return json($result);
        }
        if(empty($data['content'])){
          $result=['status'=>'0','msg'=>'请填写内容'];
          return json($result);
        }
        if(empty($data['seo_title'])){
          $result=['status'=>'0','msg'=>'请填写SEO标题'];
          return json($result);
        }

        if(empty($data['url1'])){
          $result=['status'=>'0','msg'=>'请填写url'];
          return json($result);
        }
        
        if(empty($data['newname'])){
          $result=['status'=>'0','msg'=>'请上传列表图'];
          return json($result);
        }
          
        $model=new \app\admin\model\Events();/*调用模型查询*/
        $get=$request->get();
        if(!empty($get['id'])){
          $model_data=$model->update_data($get['id'],$data['title'],$data['author'],$data['newname'],$data['content'],$data['seo_title'],$data['seo_key'],$data['seo_desc'],$data['url1'],$data['class_zhi'],$data['miaodian']);
        }else{
          $model_data=$model->insert_data($data['title'],$data['author'],$data['newname'],$data['content'],$data['seo_title'],$data['seo_key'],$data['seo_desc'],$data['url1'],$data['class_zhi'],$data['miaodian']);
        }
        if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
        }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
        }    
	  }
    
     /*删除*/
    public function del(Request $request){
        $this->check_session();
        $data=$request->post();
        if(empty($data['id'])){
            $result=['status'=>'0','msg'=>'操作有误'];
            return json($result);
        }
        $model=new \app\admin\model\Events();/*调用模型查询*/
        $model_data=$model->delete_data($data['id']);
        if($model_data!=1){
            $result=['status'=>'0','msg'=>'删除失败'];
            return json($result);
        }else{
            $result=['status'=>'1','msg'=>'删除成功'];
            return json($result);
        }
    }


}	